package org.filehanding.service;

import org.filehanding.dao.UserMapper;
import org.filehanding.entity.User;
import org.filehanding.util.PassUtil;
import org.filehanding.vo.ResponseValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by liguo on 2016/5/14.
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    public ResponseValue addUser(User user) {
        ResponseValue responseValue = new ResponseValue();
        //查询用户名是否被注册
        if (userMapper.getCountByName(user.getName()) > 0) {
            responseValue.setCode(ResponseValue.RESPONSE_CODE_NOT_ACCEPT);
            responseValue.setMessage("用户名已经被注册！");
        } else {
            //生成用户唯一GUID
            String guid = UUID.randomUUID().toString();
            user.setGuid(guid);
            //加密密码
            String newPass = PassUtil.getEncryptPassword(user.getPassword());
            user.setPassword(newPass);
            userMapper.insert(user);
            responseValue.setCode(ResponseValue.RESPONSE_CODE_SUCCESS);
        }
        return responseValue;
    }

    /**
     * 登录操作
     *
     * @param user
     * @return
     */
    public User getUser(User user) {
        //加密密码
        String newPass = PassUtil.getEncryptPassword(user.getPassword());
        user.setPassword(newPass);
        return userMapper.login(user);

    }

}
